package com.power.algorithm.structure.stack;

import java.util.Arrays;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/5/20 11:49 上午
 */
public class ArrayStack<E> {
    private int CAPACITY = 10;
    private final float EXTENSION_FACTORY = 1.5f;
    Object[] stack;
    int n = 0;
    int size = 0;

    public ArrayStack() {
        stack = new Object[CAPACITY];
    }

    public Object[] extension() {
        int newCapacity = Math.round(CAPACITY * EXTENSION_FACTORY);
        return Arrays.copyOf(stack, newCapacity);
    }

    //如果容量满了则扩容
    public void push(E e) {
        if (size == CAPACITY) {
            stack = extension();
        }
        stack[n++] = e;
        size++;
    }

    //如果没有元素  则不可弹出
    public Object pop() {
        if (size == 0) {
            return "没有元素啦!!";
        }
        size--;
        return stack[--n];
    }

    public static void main(String[] args) {
        ArrayStack<Integer> arrayStack = new ArrayStack<>();
        for (int i = 0; i < 5; i++) {
            arrayStack.push(i);
        }
        arrayStack.push(5);
        arrayStack.push(6);
        System.out.println(arrayStack.toString());
        System.out.println("arrayStack.size::" + arrayStack.size);


        for (int i = 0; i < 20; i++) {
            System.out.println(arrayStack.pop());
        }
    }
}
